package com.damage.supplier.service.impl;

import com.damage.common.config.OrderConfig;
import com.damage.common.entity.Order;
import com.damage.common.exception.IncorrectStateException;
import com.damage.common.service.IPublicService;
import com.damage.supplier.mapper.IReceiveOrderMapper;
import com.damage.supplier.service.IReceiveOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ReceiveOrderServiceImpl implements IReceiveOrderService {

    @Autowired
    private IReceiveOrderMapper mapper;

    @Autowired
    private IPublicService publicService;

    @Override
    @Transactional
    public Integer confirmOrder(Integer orderId) throws IncorrectStateException {
        Order order = publicService.findOrderById(orderId);
        if (order.getState() != OrderConfig.SENT){
            throw new IncorrectStateException("订单状态不是发送中，不能确认订单！");
        }
        return mapper.confirmOrder(orderId);
    }

    @Override
    @Transactional
    public Integer cancelOrder(Integer orderId) throws IncorrectStateException{
        Order order = publicService.findOrderById(orderId);
        if (order.getState() != OrderConfig.SENT){
            throw new IncorrectStateException("订单状态不是发送中，不能取消订单！");
        }
        return mapper.cancelOrder(orderId);
    }

    @Override
    public Integer deliverOrder(Integer orderId) throws IncorrectStateException{
        Order order = publicService.findOrderById(orderId);
        if (order.getState() != OrderConfig.CONFIRM){
            throw new IncorrectStateException("订单状态不是已确认，让订单状态处于发货中！");
        }
        return mapper.deliverOrder(orderId);
    }
}
